package com.cloud.redis7.utils;

import com.cloud.redis7.entities.Customer;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.EntityTable;
import tk.mybatis.mapper.mapperhelper.EntityHelper;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import javax.persistence.Id;
import javax.persistence.Table;
//@Component
public class DeepDebug implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {
        try {
            System.out.println("=== 深入调试实体类注解 ===");

            Class<Customer> customerClass = Customer.class;

            // 检查类级别的注解
            System.out.println("类级别注解:");
            Table tableAnnotation = customerClass.getAnnotation(Table.class);
            if (tableAnnotation != null) {
                System.out.println("  @Table name: " + tableAnnotation.name());
            } else {
                System.out.println("  没有@Table注解");
            }

            // 检查字段级别的注解
            System.out.println("\n字段级别注解:");
            Field[] fields = customerClass.getDeclaredFields();
            for (Field field : fields) {
                System.out.print("  字段: " + field.getName());

                Id idAnnotation = field.getAnnotation(Id.class);
                if (idAnnotation != null) {
                    System.out.print(" - 有@Id注解");
                } else {
                    System.out.print(" - 无@Id注解");
                }

                // 检查tk.mybatis注解 - 修正反射代码
                try {
                    Class<?> keySqlClass = Class.forName("tk.mybatis.mapper.annotation.KeySql");
                    // 使用Class<?>类型进行注解检查
                    java.lang.annotation.Annotation keySqlAnnotation = field.getAnnotation(
                            (Class<java.lang.annotation.Annotation>) keySqlClass
                    );
                    if (keySqlAnnotation != null) {
                        System.out.print(" - 有@KeySql注解");
                    }
                } catch (ClassNotFoundException e) {
                    System.out.print(" - KeySql类未找到");
                }

                System.out.println();
            }

            // 检查EntityHelper的内部状态
            System.out.println("\n=== EntityHelper内部状态 ===");
            EntityTable entityTable = EntityHelper.getEntityTable(Customer.class);

            // 尝试通过反射获取更多内部信息
            try {
                java.lang.reflect.Method getPropertyMapMethod = EntityTable.class.getDeclaredMethod("getPropertyMap");
                getPropertyMapMethod.setAccessible(true);
                Object propertyMap = getPropertyMapMethod.invoke(entityTable);
                System.out.println("PropertyMap: " + propertyMap);

                // 检查是否有其他方法可以获取主键信息
                java.lang.reflect.Method getKeyPropertiesMethod = EntityTable.class.getDeclaredMethod("getKeyProperties");
                getKeyPropertiesMethod.setAccessible(true);
                Object keyProperties = getKeyPropertiesMethod.invoke(entityTable);
                System.out.println("KeyProperties: " + keyProperties);

            } catch (Exception e) {
                System.out.println("无法通过反射获取内部信息: " + e.getMessage());
            }

            System.out.println("\n=== 结论 ===");
            System.out.println("通用Mapper存在严重bug：");
            System.out.println("1. 所有字段的isId()返回false");
            System.out.println("2. 但主键集合却包含所有字段");
            System.out.println("3. 这表明内部状态不一致，是通用Mapper的bug");

        } catch (Exception e) {
            System.err.println("调试过程中发生错误:");
            e.printStackTrace();
        }
    }
}